package test5.editor;

import robocode.editor.JavaDocument;
import robocode.editor.Keywords;

import javax.swing.event.DocumentEvent;
import javax.swing.text.*;
import java.awt.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 4, 2007
 * Time: 1:21:29 PM
 * Revision: 0
 */
public class JavaView extends PlainView
{

    public final static Color commentColor = new Color(0, 150, 0);
	public final static Color stringColor = new Color(0, 150, 150);
	public final static Color keywordColor = new Color(0, 0, 150);
	public final static Color textColor = Color.black;
	public final static int TEXT = 0;
	public final static int KEYWORD = 1;
	public final static int COMMENT = 2;
	public final static int STRING = 3;
	public final static int MULTILINECOMMENT = 4;

    
    /**
     * Constructs a new PlainView wrapped on an element.
     *
     * @param elem the element
     */
    public JavaView(Element elem)
    {
        super(elem);
    }


    protected int getTabSize()
    {
        return 4;
    }

    protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException
    {
		Document doc = getDocument();
		Segment segment = new Segment();
		Segment token = getLineBuffer();

        doc.getText(p0,p1-p0, segment);
        g.setColor(Color.red);
        g.setFont(g.getFont().deriveFont(Font.BOLD));
        x = Utilities.drawTabbedText(segment, x, y, g, this, p0);
        return x;

/*		doc.getText(p0, p1 - p0, segment);

		int count = p1 - p0;
		int left = 0;

		int state = TEXT;

		int elementIndex = doc.getDefaultRootElement().getElementIndex(p0);

		AttributeSet lineAttributes = doc.getDefaultRootElement().getElement(elementIndex).getAttributes();

		if (lineAttributes.isDefined("inComment")) {
			state = MULTILINECOMMENT;
		}

		for (int i = 0; i < count; i++) {
			// Starting in default text state.
			if (state == TEXT) {
				if (Character.isLetter(segment.array[i + segment.offset])
						&& Character.isLowerCase(segment.array[i + segment.offset])) {
					// flush now
					g.setColor(textColor);
					doc.getText(p0 + left, i - left, token);
					x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
					left = i;
					state = KEYWORD;
				} // Do nothing
				else {
					if (segment.array[i + segment.offset] == '/') {
						// flush now
						g.setColor(textColor);
						doc.getText(p0 + left, i - left, token);
						x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
						left = i;
						state = COMMENT;
					} else if (segment.array[i + segment.offset] == '"') {
						// flush now
						g.setColor(textColor);
						doc.getText(p0 + left, i - left, token);
						x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
						left = i;
						state = STRING;
					}
				}
			} else if (state == KEYWORD) {
				// Still
				if (Character.isLetter(segment.array[i + segment.offset])) {// && Character.isLowerCase(segment.array[i+segment.offset]))
					;
				} else {
					// flush now
					doc.getText(p0 + left, i - left, token);
					if (Keywords.isKeyword(token)) {
						g.setColor(keywordColor);
					} else {
						g.setColor(textColor);
					}
					x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
					left = i;
					state = TEXT;
					if (segment.array[i + segment.offset] == '/') {
						state = COMMENT;
					} else if (segment.array[i + segment.offset] == '"') {
						state = STRING;
					}
				}
			} else if (state == COMMENT) {
				if (segment.array[i + segment.offset] == '/') {
					break;
				} else if (segment.array[i + segment.offset] == '*') {
					state = MULTILINECOMMENT;
				} else {
					state = TEXT;
				}
			} else if (state == MULTILINECOMMENT) {
				if (i > 0 && segment.array[i + segment.offset] == '/' && segment.array[i + segment.offset - 1] == '*') {
					// flush now
					doc.getText(p0 + left, i + 1 - left, token);
					g.setColor(commentColor);
					x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
					left = i + 1;
					state = TEXT;
				}
			} else if (state == STRING) {
				if (segment.array[i + segment.offset] == '"') {
					// flush now
					doc.getText(p0 + left, i + 1 - left, token);
					g.setColor(stringColor);
					x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);
					left = i + 1;
					state = TEXT;
				}
			}
			// Starting not in token
		} // end loop
		// Flush last
		doc.getText(p0 + left, p1 - p0 - left, token);
		if (state == KEYWORD) {
			if (Keywords.isKeyword(token)) {
				g.setColor(keywordColor);
			} else {
				g.setColor(textColor);
			}
		} else if (state == STRING) {
			g.setColor(stringColor);
		} else if (state == COMMENT && ((p1 - p0 - left) > 1)) {
			g.setColor(commentColor);
		} else if (state == MULTILINECOMMENT) {
			g.setColor(commentColor);
		} else {
			g.setColor(textColor);
		}
		x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left);

		return x;  */

    }

    protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException
    {
        Document doc = getDocument();
        Segment segment = new Segment();
        Segment token = getLineBuffer();

        doc.getText(p0, p1 - p0, segment);

        g.setColor(Color.yellow);

        x = Utilities.drawTabbedText(segment, x, y, g, this, p0);
        return x;
    }

    public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f)
    {
        super.insertUpdate(changes, a, f);
/*        JavaDocument d = (JavaDocument) changes.getDocument();

        if (d.isNeedsRedraw())
        {
            getContainer().repaint();
            d.setNeedsRedraw(false);
        }*/
    }

    public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f)
    {
        super.removeUpdate(changes, a, f);
/*        robocode.editor.JavaDocument d = (JavaDocument) changes.getDocument();

        if (d.isNeedsRedraw())
        {
            getContainer().repaint();
            d.setNeedsRedraw(false);
        }*/
    }

    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f)
    {
        super.changedUpdate(changes, a, f);
/*        JavaDocument d = (JavaDocument) changes.getDocument();

        if (d.isNeedsRedraw())
        {
            getContainer().repaint();
            d.setNeedsRedraw(false);
        }*/
    }
}
